<?php

namespace app\api\controller\v1;

use app\lib\Validate\ProductValidate;
use app\lib\Validate\ProductRecentValidate;
use app\api\model\Product as ProductModel;
use app\api\model\Theme as ThemeModel;
use app\lib\Exception\ProductException;

class ProductController extends BaseController
{
    //获取栏目ID
    //@param $id 栏目ID
    //@param $page 当前页数
    //@param $size 每页显示几条
    //@return JSON
    public function getByProductBy($id = -1,$page = 1,$size = 30)
    {
        (new ProductValidate())->goCheck();
        //code..
    }
    
    public function getProductInfo($id)
    {
        (new ProductValidate())->goCheck();
        $result = ProductModel::getOne($id);

        if(!$result){
            throw new ProductException();
        }

        return json($result);
    }

    public function getRecent($count = 15)
    {
        (new ProductRecentValidate())->goCheck();
        $result = ProductModel::getRecentProduct($count);
        if(!$result){
            throw new ProductException([
                'message' => '新商品获取失败'
            ]);
        }

        return json($result);
    }

    public function getAll()
    {
        $result = ProductModel::all();
        if(!$result){
            throw new ProductException([
                'message' => '商品获取失败'
            ]);
        }
        
        return $result;
    }
}
